<?php
/**
 * Created by PhpStorm.
 * User: DaveTang
 * Date: 2020/3/3
 * Time: 15:56
 * 用户信息缓存组件
 */

namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Cache\Cache;

class CategoryContentCacheComponent extends Component
{

    public $_cache_config = 'category_contents';
    public function initialize(array $config)
    {
        parent::initialize($config);
    }

    /**
     * 获取内容类别下的id总数
     */
    public function getTotal($category_id, $content_type)
    {
//        $cache = Cache::read('total_'.$category_id.'_'.$content_type, $this->_cache_config);
//        if(empty($cache)){
//            $controller = $this->_registry->getController();
//            $controller->loadModel('CategoryContents');
//            $cache = $controller->CategoryContents->find()
//                ->select(['id'])
//                ->where(['category_id'=>$category_id, 'content_type'=>$content_type])
//                ->count();
//            Cache::write('total_'.$category_id.'_'.$content_type, $cache, $this->_cache_config);
//        }
//        return $cache;
    }

    /**
     * 获取分类下的内容id
     */
    public function getList($id)
    {

    }

    /**
     * 获取内容所属分类
     */
    public function getCategories($content_type, $content_id)
    {
        $key = 'category_' . $content_type.'_'.$content_id;
        $cache = Cache::read($key, $this->_cache_config);
        if(empty($cache)){
            $controller = $this->_registry->getController();
            $controller->loadModel('CategoryContents');
            $category_content = $controller->CategoryContents->find()
                ->select(['category_id'])
                ->where(['content_id'=>$content_id, 'content_type'=>$content_type])
                ->toArray();
            $cache = empty($category_content) ? [] : array_column($category_content, 'category_id');
            Cache::write($key, $cache, $this->_cache_config);
        }
        return $cache;
    }

    /**
     * 删除分类下的内容列表缓存
     */
    public function deleteList($category_id, $content_type = null)
    {
        $key = 'category_'.$category_id.'_'.$content_type;
        if(empty($content_type)){
            $controller = $this->_registry->getController();
            $controller->loadModel('CategoryContents');
            $category_content = $controller->CategoryContents->find()
                ->select(['content_type'])
                ->where(['category_id'=>$category_id])
                ->toArray();
            if(!empty($category_content)){
                foreach ($category_content as $k=>$v){
                    Cache::delete('category_'.$category_id.'_'.$v['content_type'], $this->_cache_config);
                    Cache::delete('total_'.$category_id.'_'.$content_type, $this->_cache_config);
                }
            }
        }else{
            Cache::delete($key, $this->_cache_config);
            Cache::delete('total_'.$category_id.'_'.$content_type, $this->_cache_config);
        }
        return true;
    }

    /**
     * 删除指定内容下的分类缓存
     */
    public function deleteCategories($content_type, $content_id)
    {
        $key = 'category_'.$content_type.'_'.$content_id;
        $cache = Cache::read($key, $this->_cache_config);
        if(empty($cache)){
           return true;
        }
        Cache::delete($key, $this->_cache_config);
        return true;
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：新增分类内容
   * */
    public function add($content_type, $content_id, $category_id)
    {
        if(empty($content_type)){
            $this->G->error("content_type_error");
            return null;
        }
        if(!is_numeric($content_id)){
            $this->G->error("content_id_error");
            return null;
        }
        $controller = $this->_registry->getController();
        $controller->loadModel('CategoryContents');
        $controller->CategoryContents->deleteAll(['content_type'=>$content_type,'content_id'=>$content_id]);
        $category_content = $controller->CategoryContents->newEntity();
        $category_content->content_type = $content_type;
        $category_content->content_id = $content_id;
        $category_content->category_id = $category_id;
        $controller->CategoryContents->save($category_content);
        $this->deleteList($category_id, $content_type);
        $this->deleteCategories($content_type, $content_id);
        return true;
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：删除分类内容
   * */
    public function delete($content_type, $content_id, $category_id)
    {
        if(empty($content_type)){
            $this->G->error("content_type_error");
            return null;
        }
        if(!is_numeric($content_id)){
            $this->G->error("content_id_error");
            return null;
        }
        $controller = $this->_registry->getController();
        $controller->loadModel('CategoryContents');
        $controller->CategoryContents->deleteAll(['content_type'=>$content_type,'content_id'=>$content_id]);
        $this->deleteList($category_id, $content_type);
        $this->deleteCategories($content_type, $content_id);
        return true;
    }
}